package app.project.streetlensproject.util;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import app.project.streetlensproject.R;
import app.project.streetlensproject.fragment.FgListPlace;
import app.project.streetlensproject.model.Place;

public class DialogUtils {

	public static DialogUtils instance;

	public static DialogUtils getInstance() {
		if (instance == null) {
			instance = new DialogUtils();
		}

		return instance;
	}

	public static void showQuest(Context mContext, int title, int message, int yes, int no,
			DialogInterface.OnClickListener positive, DialogInterface.OnClickListener negative) {

		String yesString = mContext.getString(yes);
		String noString = mContext.getString(no);
		String titlelString;
		if (title == 0)
			titlelString = null;
		else
			titlelString = mContext.getString(title);
		String messageString = mContext.getString(message);

		showQuest(mContext, titlelString, messageString, yesString, noString, positive, negative);
	}

	public static void showQuest(Context mContext, String title, String message, String yes, String no,
			DialogInterface.OnClickListener positive, DialogInterface.OnClickListener negative) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		if (title != null)
			builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton(yes, positive);
		builder.setNegativeButton(no, negative);
		builder.setCancelable(true).show();
	}

	public static void showInfoDialog(Context mContext, String title, String message,
			DialogInterface.OnClickListener positive) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		if (title != null)
			builder.setTitle(title);

		builder.setMessage(message);
		builder.setPositiveButton("OK", positive);
		builder.setCancelable(true).show();
	}

	public static void showInfoDialog(Context mContext, int titleID, int mesageID,
			DialogInterface.OnClickListener positive) {
		String title = null;
		if (titleID != -1)
			title = mContext.getString(titleID);
		String message = mContext.getString(mesageID);
		showInfoDialog(mContext, title, message, positive);
	}

	public static void showDialogUser(Context mContext, DialogInterface.OnClickListener listener) {
		AlertDialog.Builder builderSingle = new AlertDialog.Builder(mContext);
		builderSingle.setTitle(mContext.getString(R.string.select_function));
		final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(mContext,
				android.R.layout.select_dialog_item);
		arrayAdapter.add(mContext.getString(R.string.my_place));
		arrayAdapter.add(mContext.getString(R.string.edit_account));
		arrayAdapter.add(mContext.getString(R.string.logout));
		builderSingle.setNegativeButton(mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		builderSingle.setAdapter(arrayAdapter, listener);
		builderSingle.show();
	}

	public static void showPlaceMenuContext(Context mContext, int type, DialogInterface.OnClickListener listener) {
		AlertDialog.Builder builderSingle = new AlertDialog.Builder(mContext);
		builderSingle.setTitle(mContext.getString(R.string.select_function));
		final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(mContext,
				android.R.layout.select_dialog_item);
		if (type == FgListPlace.TYPE_SYTEM || type == FgListPlace.TYPE_APPROVE) {
			String tokenid = CommonUtils.getStringSetting(mContext, Constants.TOKEN_ID, null);
			if (tokenid != null)
				arrayAdapter.add(mContext.getString(R.string.add_to_favorite));
		} else if (type == FgListPlace.TYPE_UNAPPROVE) {
			arrayAdapter.add(mContext.getString(R.string.delete_place));
			arrayAdapter.add(mContext.getString(R.string.edit_place));
			arrayAdapter.add(mContext.getString(R.string.add_to_favorite));
		} else if (type == FgListPlace.TYPE_FAVORITE) {
			arrayAdapter.add(mContext.getString(R.string.delete_from_favorite));
		}
		builderSingle.setNegativeButton(mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		builderSingle.setAdapter(arrayAdapter, listener);
		builderSingle.show();
	}

	/**
	 * show dialog edit when go to edit screen
	 * 
	 * @param context
	 */
//	public void showDialogEdit(final Context context) {
//		final Dialog dl = new Dialog(context);
//		dl.requestWindowFeature(Window.FEATURE_NO_TITLE);
//		dl.setContentView(createViewDialogEdit(context));
//		dl.show();
//	}

	/**
	 * create view dialog edit
	 * 
	 * @param context
	 * @return
	 */
	public View createViewDialogEdit(final Context context,Place place) {
		LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = mLayoutInflater.inflate(R.layout.dialog_maker_edit, null);
		final EditText editTextName = (EditText) view.findViewById(R.id.editTextPlaceName);
		Spinner spinnerCategory = (Spinner) view.findViewById(R.id.spinnerPlaceCategory);
		final EditText editTextDescription = (EditText) view.findViewById(R.id.editTextPlaceInfor);
		ImageView  imageViewAddImage = (ImageView) view.findViewById(R.id.imageViewPlaceAddImage);
		Button buttonAdd = (Button) view.findViewById(R.id.buttonPlaceAdd);

		String[] arrCategory = context.getResources().getStringArray(R.array.arrCategory);
		ArrayAdapter<String> adp = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1,
				android.R.id.text1, arrCategory);

		spinnerCategory.setAdapter(adp);
		
		editTextName.setText(place.placename);
		editTextDescription.setText(place.description);
		imageViewAddImage.setImageBitmap(place.getBitmap());
		
		imageViewAddImage.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// go to gallery folder and add to
			}
		});

		buttonAdd.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!Utilities.getInstance().checkInputFormat(editTextName.getText().toString())
						|| !Utilities.getInstance().checkInputFormat(editTextDescription.getText().toString())) {
					Toast.makeText(context, context.getResources().getString(R.string.message_alert_input_blank),
							Toast.LENGTH_LONG).show();
				} else {
					// operation edit place
				}

			}
		});

		return view;
	}

	/**
	 * create view dialog edit
	 * 
	 * @param context
	 * @return
	 */
	public View createViewDialogAddNewPlace(final Context context, final LatLng position, final GoogleMap map,
			final Dialog dialog) {
		LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = mLayoutInflater.inflate(R.layout.dialog_maker_edit, null);
		final EditText editTextName = (EditText) view.findViewById(R.id.editTextPlaceName);
		final Spinner spinnerCategory = (Spinner) view.findViewById(R.id.spinnerPlaceCategory);
		final EditText editTextDescription = (EditText) view.findViewById(R.id.editTextPlaceInfor);
		ImageView imageViewAddImage = (ImageView) view.findViewById(R.id.imageViewPlaceAddImage);
		Button buttonAdd = (Button) view.findViewById(R.id.buttonPlaceAdd);

		String[] arrCategory = context.getResources().getStringArray(R.array.arrCategory);
		ArrayAdapter<String> adp = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1,
				android.R.id.text1, arrCategory);

		spinnerCategory.setAdapter(adp);

		imageViewAddImage.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// go to gallery folder and add to
			}
		});

		buttonAdd.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!Utilities.getInstance().checkInputFormat(editTextName.getText().toString())
						|| !Utilities.getInstance().checkInputFormat(editTextDescription.getText().toString())) {
					Toast.makeText(context, context.getResources().getString(R.string.message_alert_input_blank),
							Toast.LENGTH_LONG).show();
				} else {
					// operation edit place

					MarkerOptions mkOptionAdd = new MarkerOptions()
							.position(position)
							.title(editTextName.getText().toString())
							.snippet(editTextDescription.getText().toString())
							.icon(Utilities.getInstance().getBitmapDescriptionFromType(
									spinnerCategory.getSelectedItemPosition()));
					map.addMarker(mkOptionAdd);
					dialog.dismiss();
				}

			}
		});

		return view;
	}

}
